﻿using System.Configuration;


namespace Tab2Sql
{
    /// <summary>
    /// Class describing the options object.
    /// </summary>
    public class Tab2sqlOptions
    {
        public bool AddColumnsAsRequired;



        private SqlStatementType sqlStatementMode;

        public SqlStatementType SqlStatementMode
        {
            get { return sqlStatementMode; }
            set { sqlStatementMode = value; }
        }


        bool recordBatchProfiles;

        public bool RecordBatchProfiles
        {
            get { return recordBatchProfiles; }
            set { recordBatchProfiles = value; }
        }


        bool outputToFile;

        public bool OutputToFile
        {
            get { return outputToFile; }
            set { outputToFile = value; }
        }

        private int batchSize;
        private uint maxFileSizeInBatchMode;
        private string tablePrefixInBatchMode;

        public string TablePrefixInBatchMode
        {
            get { return tablePrefixInBatchMode; }
            set { tablePrefixInBatchMode = value; }
        }

        public uint MaxFileSizeInBatchMode
        {
            get { return maxFileSizeInBatchMode; }
            set { maxFileSizeInBatchMode = value; }
        }

        public SqlVariants SqlVariant;

        /// <summary>
        /// Datatype of additional column.
        /// </summary>
        public string AddCol1DataType = "";

        /// <summary>
        /// Additional column number one.
        /// </summary>
        public string AddCol1Name = "";

        /// <summary>
        /// Datatype of additional column #2.
        /// </summary>
        public string AddCol2DataType = "";

        /// <summary>
        /// Additional column number two.
        /// </summary>
        public string AddCol2Name = "";

        /// <summary>
        /// Should the program try to guess what the delimiter is, if it does't occur in the first row?
        /// </summary>
        public bool AutoDetectDelimiter;

        /// <summary>
        /// How much should we 'fix' column headings to make them sql safe?
        /// </summary>
        public HandlingOfColumnNames columnNameModel;

        /// <summary>
        /// Delimiter by default is tab, but can be pipe, comma, etc.
        /// </summary>
        public string Delimiter;

        /// <summary>
        /// Should we use FirstRowHeaders?
        /// </summary>
        public bool FirstRowIsHeader;

        /// <summary>
        /// Should we ignore lines beginning with a hash?
        /// </summary>
        public bool IgnoreComments;

        /// <summary>
        /// Should we include a row number with our results? Default = yes.
        /// </summary>
        public bool IncludeRowNumber;

        

        /// <summary>
        /// Table name. By default this is #foo.
        /// </summary>
        public string TableName;

        /// <summary>
        /// Meh;
        /// </summary>
        public string UserDelimiter;

        /// <summary>
        /// constructor!
        /// </summary>
        public Tab2sqlOptions()
        {
            SqlVariant = new SqlVariants();
            
            initDefaults();
        }
        public Tab2sqlOptions(SqlVariants desiredVariant)
        {
            SqlVariant = desiredVariant;
            initDefaults();
        }
        private bool includeQueryFooter;

        public bool IncludeQueryFooter
        {
            get { return includeQueryFooter; }
            set { includeQueryFooter = value; }
        }
        public int BatchSize
        {
            get { return batchSize; }
            set { batchSize = value; }

        }

        private bool folderMode;

        public bool FolderMode
        {
            get { return folderMode; }
            set { folderMode = value; }
        }


        private bool suppressBatchStuff;

        public bool SuppressBatchStuff
        {
            get { return suppressBatchStuff; }
            set { suppressBatchStuff = value; }
        }
        private OutputTypes outputMode;

        public OutputTypes OutputMode
        {
            get { return outputMode; }
            set { outputMode = value; }
        }

        

        private void initDefaults()
        {
            Assemblage asm = new Assemblage();
            string thisVersion = asm.AssemblyVersion;
            if (thisVersion != Properties.Settings.Default.Assembly_String)
            {
                Properties.Settings.Default.Upgrade();
            }
            try
            {
                //SqlStatementMode = Properties.Settings.Default.SqlStatementType.Standard;
                sqlStatementMode = SqlStatementType.Standard;
                //Properties.Settings.Default.Reload
                maxFileSizeInBatchMode = Properties.Settings.Default.MaximumFileSizeInBatchMode;
                BatchSize = Properties.Settings.Default.BatchSize;
                Delimiter = Properties.Settings.Default.Delimiter;
                includeQueryFooter = Properties.Settings.Default.IncludeQueryFooter;
                SqlVariant = Properties.Settings.Default.SqlVariant;
            }
            catch
            {
                //TODO : Warn user that invalid setting exists

                maxFileSizeInBatchMode = 5000000;
                BatchSize = 100;
                Delimiter = "\t";
                tablePrefixInBatchMode = "tab2sql_";
                includeQueryFooter = false;
            }
            AddColumnsAsRequired = true;
            TableName = "#foo";
            AutoDetectDelimiter = true;
            FirstRowIsHeader = true;
            IgnoreComments = true;
            IncludeRowNumber = true;
            outputMode = OutputTypes.Default;
            suppressBatchStuff = false;
            folderMode = false;
            outputToFile = false;
            recordBatchProfiles = includeQueryFooter;

        }
    }
}